package com.example.springboot_log.Test;


import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.util.UUID;

//七牛云
class TestObjecStorageTest {
    //not me
    public static String testPrint(String localFilePath) {
        //构造一个带指定 Region 对象的配置类
        //Region.region2()对应创建的空间区域，刚刚创建空间我使用了华南，对应region2();
        Configuration cfg = new Configuration(Region.region2());
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        //...生成上传凭证，然后准备上传
        String accessKey = "BITG3xjH_kr2w7uxDiMW1udeIthyJqhLENtf-4Ks";
        String secretKey = "yjrgG_1AhJVQt4fFrb4Vd5y6Lslz7ba06fv-gaPR";
        String bucket = "dxj1016";//空间名称
        //如果是Windows情况下，格式是 D:\\qiniu\\test.png;本地文件路径
        //默认不指定key的情况下，以文件内容的hash值作为文件名
        String key = null;
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(localFilePath, key, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
            String url = "http://qu6hzlc6e.hn-bkt.clouddn.com/" + putRet.hash;
            return url;
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }
        return null;
    }

    public static String GetImageStr() {//将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        String imgFile = "D:\\赵丽颖\\微信图片_20200924135436.jpg";//待处理的图片
        InputStream in = null;
        byte[] data = null;
        //读取图片字节数组
        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);//返回Base64编码过的字节数组字符串
    }

    public static String GenerateImagebendi(String imgStr) {  //对字节数组字符串进行Base64解码并生成图片
        if (imgStr == null) //图像数据为空
            return null;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            //Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {//调整异常数据
                    b[i] += 256;
                }
            }
            String imgFilePath = "D:\\赵丽颖\\" + System.currentTimeMillis() + UUID.randomUUID().toString().replace(".", "").substring(0, 6) + ".jpg";//新生成的图片
//            String pic = testPrint(imgFilePath);
//            String newfilename = System.currentTimeMillis() + UUID.randomUUID().toString().replace(".", "").substring(0, 6) + ".jpg" ;
//            String fileLocation = "/static/restaurantRes/";
//            String filePath = "http://localhost:8085"
//                    + fileLocation + newfilename;
            String filePath = testPrint(imgFilePath);
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            return filePath;
        } catch (Exception e) {
            return null;
        }
    }

    public static void main(String[] args) {
        String strImg = GetImageStr();
        String pic = GenerateImagebendi(strImg);
        System.out.println(pic);
        /*
        测试的时候，发现字符串过程出现错误
        */
    }
}
